package com.sumainfor.wx.tenant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sumainfor.common.base.ITenantSelect;
import com.sumainfor.common.entity.WxUserEntity;
import com.sumainfor.common.exception.TCException;
import com.sumainfor.wx.tenant.dao.TctenTenantMapper;
import com.sumainfor.wx.tenant.dao.TctenTenantWxMapper;
import com.sumainfor.wx.tenant.entity.TctenTenant;
import com.sumainfor.wx.tenant.entity.TctenTenantIdentit;
import com.sumainfor.wx.tenant.entity.TctenTenantWx;
import com.sumainfor.wx.tenant.service.TctenTenantIdentitService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author Jack Chen
 * @Create 2019-01-28 15:31
 */
@Service
public class TenantSelectImpl implements ITenantSelect {

    @Autowired
    private TctenTenantWxMapper wxMapper;

    @Autowired
    private TctenTenantMapper userMapper;

    @Autowired
    private TctenTenantIdentitService cardService;

    @Override
    public WxUserEntity getWxBaseInfo(String openId) {

        if (StringUtils.isBlank(openId)) {
            throw new TCException("NotFont OpenId");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("openid", openId);
        return getWxInfoByWhere(queryWrapper);
    }

    /**
     * 通过TenID 获得租客的数据
     *
     * @param tenId
     * @return
     */
    @Override
    public WxUserEntity getWxBaseInfoByTenId(String tenId) {
        if (StringUtils.isBlank(tenId)) {
            throw new TCException("NotFont TenId");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("TenId", tenId);
        return getWxInfoByWhere(queryWrapper);
    }

    private WxUserEntity getWxInfoByWhere(QueryWrapper queryWrapper) {
        TctenTenantWx tenantWx = null;
        try {
            tenantWx = wxMapper.selectOne(queryWrapper);
        } catch (Exception t) {
            throw new TCException("不能存在多个,数据紊乱");
        }

        if (tenantWx == null) {
            return null;
        }

        queryWrapper = new QueryWrapper();
        queryWrapper.eq("TenId", tenantWx.getTenId());
        TctenTenant tenant = userMapper.selectOne(queryWrapper);
        if (tenant == null) {
            return null;
        }

        queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("TenId", tenantWx.getTenId());
        //获取身份证信息
        TctenTenantIdentit cardEntity = cardService.getOne(queryWrapper);

        WxUserEntity entity = new WxUserEntity();
        entity.setTenId(tenant.getTenId());
        entity.setTenPhone(tenant.getTenPhone());
        entity.setTenName(tenant.getTenName());
        entity.setTenImg(tenant.getTenImg());
        entity.setTenSex(tenant.getTenSex());
        entity.setUsrType(tenant.getUsrType());
        entity.setTenSerpad(tenant.getTenSerpad());
        entity.setOpenid(tenantWx.getOpenid());
        entity.setUnionid(tenantWx.getUnionid());
        entity.setSubscribe(tenantWx.getSubscribe());
        entity.setPactAccountId(tenant.getPactAccountId());
        if (cardEntity != null) {
            entity.setCardId(cardEntity.getIDNumber());
            entity.setRealName(cardEntity.getRealname());
        }
        return entity;
    }
}
